package hqu.com.homework6;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    // 定义界面上的两个文本框
    EditText input, show;
    // 定义界面上的一个按钮
    Button send;
    OutputStream os;
    Handler handler;

    @SuppressLint("WrongViewCast")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        input = findViewById(R.id.input);
        send = findViewById(R.id.send);
        show = findViewById(R.id.show);
        Socket s;
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                // 如果消息来自于子线程
                if (msg.what == 0x123) {
                    // 将读取的内容追加显示在文本框中
                    show.append("\n" + msg.obj.toString());
                }
            }
        };
        try {
            s = new Socket("               ", 40000);
            // 客户端启动ClientThread线程不断读取来自服务器的数据
            new Thread(new ClientThread(s, handler)).start();  // ①
            os = s.getOutputStream();
        } catch (Exception e) {
            e.printStackTrace();
        }
        send.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    // 将用户在文本框内输入的内容写入网络
                    os.write((input.getText().toString() + "\r\n")
                            .getBytes("utf-8"));
                    // 清空input文本框
                    input.setText("");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public class ClientThread implements Runnable {
        //该线程负责处理的Socket
        private Socket s;
        private Handler handler;
        //该线程所处理的Socket所对应的输入流
        BufferedReader br = null;

        public ClientThread(Socket s, Handler handler) throws IOException {
            this.s = s;
            this.handler = handler;
            br = new BufferedReader(
                    new InputStreamReader(s.getInputStream()));
        }

        public void run() {
            try {
                String content = null;
                //不断读取Socket输入流中的内容。
                while ((content = br.readLine()) != null) {
                    // 每当读到来自服务器的数据之后，发送消息通知程序界面显示该数据
                    Message msg = new Message();
                    msg.what = 0x123;
                    msg.obj = content;
                    handler.sendMessage(msg);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

